package com.zivwu.library.ui;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.zivwu.library.R;
import com.zivwu.library.dao.bean.NavBean;
import com.zivwu.library.ui.base.IActivity;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.List;

public abstract class BaseIndexActivity extends IActivity {

    protected MagicIndicator magicIndicator;
    FragmentContainerHelper fragmentContainerHelper;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLayoutRes() > 0) {
            setContentView(getLayoutRes());
        } else {
            setContentView(R.layout.ziv_nav_fragment_activity);
        }
        init();
    }

    protected abstract
    @LayoutRes
    int getLayoutRes();

    private void init() {
        magicIndicator = (MagicIndicator) findViewById(R.id.ziv_nav_magicIndicator);
        fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        initIndicator();
    }

    protected void initIndicator() {
        CommonNavigator navigator = new CommonNavigator(this);
        navigator.setAdapter(new IndexNavigatorAdapter(getNavBean()));
        magicIndicator.setNavigator(navigator);
        fragmentContainerHelper.attachMagicIndicator(magicIndicator);
    }

    protected abstract
    @NonNull
    List<NavBean> getNavBean();


    public void selectFragment(int index) {
        fragmentContainerHelper.handlePageSelected(index);
        switchPages(index, R.id.ziv_nav_fragment_container, getFragment());
    }

    protected abstract Fragment[] getFragment();

    private void switchPages(int index, @IdRes int containerViewId, Fragment[] navFragments) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        if (navFragments[index] == null)
            initFragment(index, navFragments);
        //隐藏所有fragment
        for (int i = 0; i < navFragments.length; i++) {
            if (i == index) {
                continue;
            }
            fragment = navFragments[i];
            if (fragment != null && fragment.isAdded()) {
                fragmentTransaction.hide(fragment);
            }
        }

        fragment = navFragments[index];
        //已被添加到fragmentTransaction ，显示fargment ,否则添加
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(containerViewId, fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
    }

    protected abstract void initFragment(int index, Fragment[] navFragments);


}
